﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ADNColorPicker;
using ADNTimeline;
using ADNCanvas;
using ADNLib;
using ADNActions;

namespace ADN
{
	public partial class ADNMain : Form
	{
		private FileManager fileMan = new FileManager();
		private ColorPickerDialog colorDialog = null;
        private FormMotions motionDialog = null;
        private FontEditorDialog fontDialog = null;

		public ADNMain()
		{
			InitializeComponent();
			Initialize();
		}

		private void Initialize()
		{
			ToolStripManager.Renderer = new ToolStripProfessionalRenderer(new ADNToolStripColorTable(toolMain.BackColor));
		}

        private void ADNMain_Load(object sender, EventArgs e)
        {
            EnableInput(false);
        }

		#region DrawingToolbar

		private void btnPointer_Click(object sender, EventArgs e)
		{
            if (canvas1.Mode == DrawingMode.Drawing) { canvas1.DrawingTool = new ToolPointer(); }
            else { canvas1.DrawingTool = new ToolAnimPointer(); }
			UnCheckTools(btnPointer);
		}

		private void btnPointEdit_Click(object sender, EventArgs e)
		{
            if (canvas1.Mode == DrawingMode.Drawing) { canvas1.DrawingTool = new ToolPointEdit(); }
            else { canvas1.DrawingTool = new ToolAnimPointEdit(); }
			
			UnCheckTools(btnPointEdit);
		}

		private void btnRectangle_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolDiPoint();
			canvas1.DrawingTool.ToolType = DrawingToolType.Rectangle;
			UnCheckTools(btnRectangle);
		}

		private void btnEllipse_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolDiPoint();
			canvas1.DrawingTool.ToolType = DrawingToolType.Ellipse;
			UnCheckTools(btnEllipse);
		}

		private void btnLine_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolDiPoint();
			canvas1.DrawingTool.ToolType = DrawingToolType.Line;
			UnCheckTools(btnLine);
		}

		private void btnBezier_Click(object sender, EventArgs e)
		{
            if (canvas1.Mode == DrawingMode.Drawing) { canvas1.DrawingTool = new ToolBezier(DrawingToolType.Bezier); }
            else { canvas1.DrawingTool = new ToolAnimPath(DrawingToolType.AnimPath); }

			UnCheckTools(btnBezier);
		}

		private void btnText_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolText();
			UnCheckTools(btnText);
		}

		private void btnFreehand_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolFreehand(DrawingToolType.Freehand);
			UnCheckTools(btnFreehand);
		}

		private void btnSpray_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolFreehand(DrawingToolType.Spray);
			UnCheckTools(btnSpray);
		}

		private void btnRightTriangle_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.RightTriangle);
            UnCheckTools(btnRightTriangle);
		}

		private void btnIsoscelesTriangle_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.IsoscelesTriangle);
            UnCheckTools(btnIsoscelesTriangle);
		}

		private void btnPentagon_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.Pentagon);
            UnCheckTools(btnPentagon);
		}

		private void btnHexagon_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.Hexagon);
            UnCheckTools(btnHexagon);
        }

        private void btnOctagon_Click(object sender, EventArgs e)
        {
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.Octagon);
            UnCheckTools(btnOctagon);
        }

		private void btnParallelogram_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.Parallelogram);
            UnCheckTools(btnParallelogram);
		}

		private void btnTrapezoid_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.Trapezoid);
            UnCheckTools(btnTrapezoid);
		}

		private void btn5PointStar_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.FivePointStar);
            UnCheckTools(btn5PointStar);
		}

		private void btn6PointStar_Click(object sender, EventArgs e)
		{
            canvas1.DrawingTool = new ToolDiPoint(DrawingToolType.SixPointStar);
            UnCheckTools(btn6PointStar);
		}

		private void btnClone_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolClone();
			UnCheckTools(btnClone);
		}

		private void btnShadow_Click(object sender, EventArgs e)
		{
			canvas1.DrawingTool = new ToolShadow();
			UnCheckTools(btnShadow);
		}

		private void btnLockTool_Click(object sender, EventArgs e)
		{
			canvas1.ToolLocked = btnLockTool.Checked;
		}

		private void UnCheckTools(object checkedTool)
		{
            if (checkedTool.GetType() == typeof(ToolStripButton))
            {
                ToolStripButton tb = (ToolStripButton)checkedTool;
                tb.Checked = true;  //make sure you can't uncheck a tool by clicking it twice.
            }

			if (btnPointer != checkedTool) { btnPointer.Checked = false; }
			if (btnPointEdit != checkedTool) { btnPointEdit.Checked = false; }
			if (btnRectangle != checkedTool) { btnRectangle.Checked = false; }
			if (btnEllipse != checkedTool) { btnEllipse.Checked = false; }
			if (btnLine != checkedTool) { btnLine.Checked = false; }
			if (btnBezier != checkedTool) { btnBezier.Checked = false; }
			if (btnText != checkedTool) { btnText.Checked = false; }
			if (btnFreehand != checkedTool) { btnFreehand.Checked = false; }
			if (btnSpray != checkedTool) { btnSpray.Checked = false; }
			if (btnClone != checkedTool) { btnClone.Checked = false; }
			if (btnShadow != checkedTool) { btnShadow.Checked = false; }

			if (btnRightTriangle != checkedTool) { btnRightTriangle.Checked = false; }
			if (btnIsoscelesTriangle != checkedTool) { btnIsoscelesTriangle.Checked = false; }
			if (btnPentagon != checkedTool) { btnPentagon.Checked = false; }
			if (btnHexagon != checkedTool) { btnHexagon.Checked = false; }
			if (btnOctagon != checkedTool) { btnOctagon.Checked = false; }
			if (btnParallelogram != checkedTool) { btnParallelogram.Checked = false; }
			if (btnTrapezoid != checkedTool) { btnTrapezoid.Checked = false; }
			if (btn5PointStar != checkedTool) { btn5PointStar.Checked = false; }
			if (btn6PointStar != checkedTool) { btn6PointStar.Checked = false; }
		}

		#endregion

		#region MainToolbar

		private void btnNewMovie_Click(object sender, EventArgs e)
		{
            FormNew fn = new FormNew();
            DialogResult res = fn.ShowDialog(this);

            if (res == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;

                if (fileMan == null) { fileMan = new FileManager(); }
                Size docSize = new Size((int)fn.ResolutionWidth, (int)fn.ResolutionHeight);
                fileMan.Create(docSize, (int)fn.FPS);
              
                LoadAnimation();

                EnableInput(true);
                canvas1.Document = docSize;
                canvas1.SetZoom(canvas1.View);
                canvas1.Focus();
                this.Cursor = Cursors.Default;
            }
		}	
	
        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (fileMan == null) { fileMan = new FileManager(); }  
            fileMan.Open();
            LoadAnimation();
            
            EnableInput(true);
			canvas1.SetZoom(canvas1.View);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (fileMan != null) { fileMan.Save(); }
        }

        private void EnableInput(bool enable)
        {
            splitMain.Visible = enable;
            toolDrawing.Enabled = enable;

            btnSave.Enabled = enable;
            btnCut.Enabled = enable;
            btnCopy.Enabled = enable;
            btnPaste.Enabled = enable;
            btnUndo.Enabled = enable;
            btnRedo.Enabled = enable;
            btnDocProperties.Enabled = enable;
            btnColor.Enabled = enable;
            btnTimeline.Enabled = enable;
            btnAnimationMode.Enabled = enable;
            btnShapeNavigator.Enabled = enable;
            btnZoom.Enabled = enable;
            btnOrderDropDown.Enabled = enable;
            btnGroupDropDown.Enabled = enable;
            btnAlignDropdown.Enabled = enable;
            btnRotateDropdown.Enabled = enable;
            btnDrawConstrained.Enabled = enable;
            btnEffects.Enabled = enable;

            mnuEdit.Enabled = enable;
            mnuView.Enabled = enable;
            mnuObject.Enabled = enable;
            mnuLayer.Enabled = enable;

            //file menu
            mnuSave.Enabled = enable;
            mnuSaveAs.Enabled = enable;
            mnuImport.Enabled = enable;
            mnuRender.Enabled = enable;

        }

        private void LoadAnimation()
        {
            if (fileMan.Movie != null && fileMan.Movie.Layers.Count > 0)
            {
                //TODO: move to LoadTimeline function
                timeline.Items.Clear();

                for (int i = 0; i < fileMan.Movie.Layers.Count; i++)
                {
                    FrameItem fi = new FrameItem();

                    for (int j = 0; j < fileMan.Movie.Layers[i].Frames.Count; j++)
                    {
                        TimeLineFrame tlf = new TimeLineFrame();
                        tlf.StartCell = fileMan.Movie.Layers[i].Frames[j].StartFrame;
                        tlf.Span = fileMan.Movie.Layers[i].Frames[j].Span;

                        fi.Frames.Add(tlf);
                    }

                    LayerItem li = new LayerItem();
                    TimelineItem tli = new TimelineItem(li, fi);
                    timeline.Items.Add(tli);
                }

                canvas1.WorkingFrame = fileMan.Movie.Layers[0].Frames[0];
            }
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
			if (canvas1.SelectedObjects.Count > 0)
			{
				IDataObject ido = new DataObject();
				ido.SetData("ADNOBJECTLIST", AnimIO.CloneObject(canvas1.SelectedObjects));
				Clipboard.SetDataObject(ido);

				Edit.Cut(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
				canvas1.ForceRedraw = true;
				canvas1.Invalidate();
			}
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (canvas1.SelectedObjects.Count > 0)
            {
                IDataObject ido = new DataObject();
                ido.SetData("ADNOBJECTLIST", AnimIO.CloneObject(canvas1.SelectedObjects));
                Clipboard.SetDataObject(ido);
            }
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            IDataObject ido = Clipboard.GetDataObject();
            if (ido.GetDataPresent("ADNOBJECTLIST"))
            {
                List<XObject> pasteObjs = (List<XObject>)Clipboard.GetData("ADNOBJECTLIST");
                if (pasteObjs != null && pasteObjs.Count > 0)
                {
                    canvas1.WorkingFrame.XObjects.AddRange(pasteObjs);
					canvas1.ForceRedraw = true;
					canvas1.Invalidate();
                }
            }
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {

        }

        private void btnRedo_Click(object sender, EventArgs e)
        {

        }

        private void btnDocProperties_Click(object sender, EventArgs e)
        {

        }

        private void btnColor_Click(object sender, EventArgs e)
        {
			if (colorDialog == null || colorDialog.IsDisposed) 
			{
				CreateColorDialog();
			}
			else if (colorDialog.Visible == false)
			{
				colorDialog.Show(this);
			}

			if (canvas1.SelectedObjects.Count > 0 && canvas1.SelectedObjects[0].Type == XObjectType.Shape)
			{
				Shape s = (Shape)canvas1.SelectedObjects[0];
				colorDialog.LineColor = s.LineColor.GetColors();
				colorDialog.FillColor = s.FillColor.GetColors();
				colorDialog.LineSettings.Width = s.LineSettings.Width;
			}
        }

		private void CreateColorDialog()
		{
			colorDialog = new ColorPickerDialog();
			colorDialog.Owner = this;
			colorDialog.StartPosition = FormStartPosition.Manual;
			Point startPoint = new Point(this.Left, this.Bottom - colorDialog.Height);
			startPoint.Offset(15, -15);
			colorDialog.Location = startPoint;
			colorDialog.ColorChanged += new EventHandler(ColorDialog_ColorChanged);
			colorDialog.Show(this);
		}

        private void btnToolSettings_Click(object sender, EventArgs e)
        {

        }

        private void btnTextProperties_Click(object sender, EventArgs e)
        {
            if (fontDialog == null || fontDialog.IsDisposed)
            {
                CreateTextDialog();
            }
            else if (fontDialog.Visible == false)
            {
                fontDialog.Show();
            }

            if (canvas1.SelectedObjects.Count > 0 && canvas1.SelectedObjects[0].GetType() == typeof(ShapeText))
            {
                ShapeText st = (ShapeText)canvas1.SelectedObjects[0];
                fontDialog.FontText = st.Text;
                fontDialog.FontName = st.TextFont.GetFamily().ToString();
                fontDialog.FontSize = st.TextFont.Size;

                //TODO: setting current font properties.
            }
        }

        private void CreateTextDialog()
        {
            fontDialog = new FontEditorDialog();
            fontDialog.Owner = this;
            fontDialog.StartPosition = FormStartPosition.Manual;
            Point startPoint = new Point(this.Right - fontDialog.Width, this.Top);
            startPoint.Offset(15, -15);
            fontDialog.Location = startPoint;
            fontDialog.FontChanged += new EventHandler(FontDialog_FontChanged);
            fontDialog.Show(this);
        }

        void FontDialog_FontChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

		private void btnTimeline_Click(object sender, EventArgs e)
        {
            splitMain.Panel1Collapsed = !btnTimeline.Checked;
        }

        private void btnAnimationMode_Click(object sender, EventArgs e)
        {
            EnableAnimationMode(btnAnimationMode.Checked);
        }

        private void EnableAnimationMode(bool enable)
        {
            if (enable) { canvas1.Mode = DrawingMode.Animation; }
            else { canvas1.Mode = DrawingMode.Drawing; }

            btnEllipse.Enabled = !enable;
            btnLine.Enabled = !enable;
            btnText.Enabled = !enable;
            btnFreehand.Enabled = !enable;
            btnSpray.Enabled = !enable;
            btnPattern.Enabled = !enable;
            btnClone.Enabled = !enable;
            btnShadow.Enabled = !enable;
            btnLockTool.Enabled = !enable;

            mnuAnimation.Visible = enable;

            if (enable && colorDialog != null) { colorDialog.Hide(); }
            if (!enable && motionDialog != null) { motionDialog.Hide(); }

        }

        private void btnShapeNavigator_Click(object sender, EventArgs e)
        {
            if (canvas1.WorkingFrame != null)
            {
                FormShapeNav fsn = new FormShapeNav();
                fsn.WorkingFrame = canvas1.WorkingFrame;
                fsn.SelectionChanged += new EventHandler(ShapeNavigator_SelectionChanged);
                fsn.ObjectNameChanged += new EventHandler(ShapeNavigator_ObjectNameChanged);
                fsn.Show(this);
            }
            
        }

        private void btnZoom_ButtonClick(object sender, EventArgs e)
        {
            canvas1.SetZoom(canvas1.View);  //default action is zoom document
        }

        private void btnZoomDoc_Click(object sender, EventArgs e)
		{
			canvas1.SetZoom(canvas1.View);
		}

		private void btnZoomSelection_Click(object sender, EventArgs e)
		{
			
		}

		private void btnZoomPercent_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem btn = (ToolStripMenuItem)sender;
			double zoom = Double.Parse((string)btn.Tag) / 100.00d;

			canvas1.SetZoom((float)zoom);
		}

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
			Order.MoveUp(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnMoveTop_Click(object sender, EventArgs e)
        {
            Order.MoveTop(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            Order.MoveDown(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnMoveBottom_Click(object sender, EventArgs e)
        {
            Order.MoveBottom(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnGroup_Click(object sender, EventArgs e)
        {
			Group.GroupSelected(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnUngroup_Click(object sender, EventArgs e)
        {
			Group.UngroupSelected(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnUngroupAll_Click(object sender, EventArgs e)
        {
			Group.UngroupAll(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnAlignLeft_Click(object sender, EventArgs e)
        {
            Align.AlignLeft(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnAlignCenter_Click(object sender, EventArgs e)
        {
            Align.AlignCenter(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnAlignRight_Click(object sender, EventArgs e)
        {
            Align.AlignRight(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnAlignTop_Click(object sender, EventArgs e)
        {
            Align.AlignTop(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnAlignMiddle_Click(object sender, EventArgs e)
        {
            Align.AlignMiddle(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnAlignBottom_Click(object sender, EventArgs e)
        {
            Align.AlignBottom(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void mnuDistributeHoriz_Click(object sender, EventArgs e)
        {
            Align.DistributeHoriz(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void mnuDistributeVert_Click(object sender, EventArgs e)
        {
            Align.DistributeVert(canvas1.SelectedObjects);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnSimplify_Click(object sender, EventArgs e)
        {

        }

        private void btnOutline_Click(object sender, EventArgs e)
        {

        }

        private void btnUnion_Click(object sender, EventArgs e)
        {

        }

        private void btnDifference_Click(object sender, EventArgs e)
        {

        }

        private void btnIntersection_Click(object sender, EventArgs e)
        {

        }

        private void btnExclusion_Click(object sender, EventArgs e)
        {

        }

        private void btnDivision_Click(object sender, EventArgs e)
        {

        }

        private void btnFlipHoriz_Click(object sender, EventArgs e)
        {
            Rotate.FlipHoriz(canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnFlipVertical_Click(object sender, EventArgs e)
        {
            Rotate.FlipVert(canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnMirrorVertTop_Click(object sender, EventArgs e)
        {
            Rotate.MirrorVert(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects, MirrorDirection.Top);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnMirrorVertBottom_Click(object sender, EventArgs e)
        {
			Rotate.MirrorVert(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects, MirrorDirection.Bottom);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnMirrorHorizRight_Click(object sender, EventArgs e)
        {
            Rotate.MirrorHoriz(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects, MirrorDirection.Right);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnMirrorHorizLeft_Click(object sender, EventArgs e)
        {
			Rotate.MirrorHoriz(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects, MirrorDirection.Left);
			canvas1.ForceRedraw = true;
			canvas1.Invalidate();
        }

        private void btnRotate90CW_Click(object sender, EventArgs e)
        {
            Rotate.Rotate90(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnRotate90Rev_Click(object sender, EventArgs e)
        {
            Rotate.Rotate90Rev(canvas1.WorkingFrame.XObjects, canvas1.SelectedObjects);
            canvas1.ForceRedraw = true;
            canvas1.Invalidate();
        }

        private void btnConstrainInterior_Click(object sender, EventArgs e)
        {

        }

        private void btnConstrainExterior_Click(object sender, EventArgs e)
        {

        }

        private void btnDropShadow_Click(object sender, EventArgs e)
        {

        }

        private void btnBlur_Click(object sender, EventArgs e)
        {

        }

		#endregion

		#region ColorDialogEvents

		void ColorDialog_ColorChanged(object sender, EventArgs e)
		{
			if (canvas1.SelectedObjects != null)
			{
				for (int i = 0; i < canvas1.SelectedObjects.Count; i++)
				{
					if (canvas1.SelectedObjects[i].Type == XObjectType.Shape)
					{
						Shape s = (Shape)canvas1.SelectedObjects[i];

						if (colorDialog.FillColor != null)
						{
							s.FillColor.SetColors(colorDialog.FillColor);
							if (colorDialog.FillColor.Colors.Length > 1) { s.FillColor.Type = ColorType.LinearGradient; }
							else { s.FillColor.Type = ColorType.Solid; }
							s.FillColor.Angle = colorDialog.Angle;
						}

						if (colorDialog.LineColor != null)
						{
							s.LineColor.SetColors(colorDialog.LineColor);
							if (colorDialog.LineColor.Colors.Length > 1) { s.LineColor.Type = ColorType.LinearGradient; }
							else { s.LineColor.Type = ColorType.Solid; }
							s.LineColor.Angle = colorDialog.Angle;
						}

                        if (colorDialog.LineSettings != null)
                        {
                            s.LineSettings.Width = colorDialog.LineSettings.Width;
                        }
					}
					else if (canvas1.SelectedObjects[i].Type == XObjectType.Group)
					{
						//loop through children OR add fill and line color to group - should return an average
						//and set all children on SET
					}
				}

				canvas1.ForceRedraw = true;
				canvas1.Invalidate();
			}
		}

		#endregion

		#region ShapeNavigatorEvents

		void ShapeNavigator_ObjectNameChanged(object sender, EventArgs e)
		{

		}

		void ShapeNavigator_SelectionChanged(object sender, EventArgs e)
		{

		}

		#endregion

		#region TimelineEvents

        private void timeline_SelectionChanged(object sender, TimelineEventArgs e)
        {
            if (fileMan != null)
            {
                if (e.FrameIndex >= 0 && e.LayerIndex >= 0)
                {
                    canvas1.WorkingFrame = fileMan.Movie.Layers[e.LayerIndex].Frames[e.FrameIndex];
                }
            }
        }

		private void timeline_FrameAdded(object sender, TimelineEventArgs e)
		{
            if (fileMan != null)
            {
                Frame f = new Frame(e.FrameStart, e.FrameSpan);
                if (e.FrameIndex >= fileMan.Movie.Layers[e.LayerIndex].Frames.Count)
                {
                    fileMan.Movie.Layers[e.LayerIndex].Frames.Add(f);
                }
                else { fileMan.Movie.Layers[e.LayerIndex].Frames.Insert(e.FrameIndex, f); }
            }
		}

		//private void timeline_FrameLoaded(object sender, TimelineEventArgs e)
		//{
		//    if (fileMan != null)
		//    {
		//        canvas1.WorkingFrame = fileMan.Movie.Layers[e.LayerIndex].Frames[e.FrameIndex];
		//    }
		//}

		private void timeline_FrameMoved(object sender, TimelineEventArgs e)
		{
			if (fileMan != null)
			{
				fileMan.Movie.Layers[e.LayerIndex].Frames[e.FrameIndex].StartFrame = e.FrameStart;
				fileMan.Movie.Layers[e.LayerIndex].Frames[e.FrameIndex].Span = e.FrameSpan;
			}
		}

		private void timeline_FrameRemoved(object sender, TimelineEventArgs e)
		{

		}

		private void timeline_FrameDuplicated(object sender, TimelineEventArgs e)
		{
            if (fileMan != null)
            {
                Frame f = (Frame)AnimIO.CloneObject(fileMan.Movie.Layers[e.LayerIndex].Frames[e.FrameIndex]);
                fileMan.Movie.Layers[e.LayerIndex].Frames.Add(f);
            }
		}

		private void timeline_FramesShifted(object sender, TimelineEventArgs e)
		{

		}

		private void timeline_LayerAdded(object sender, TimelineEventArgs e)
		{
            if (fileMan != null)
            {
                fileMan.Movie.Layers.Add(new Layer());
            }
		}

		private void timeline_LayerLockChanged(object sender, TimelineEventArgs e)
		{

		}

		private void timeline_LayerMoved(object sender, TimelineEventArgs e)
		{

		}

		private void timeline_LayerRemoved(object sender, TimelineEventArgs e)
		{

		}

		private void timeline_LayerVisibleChanged(object sender, TimelineEventArgs e)
		{

		}

		private void timeline_OnionSkinChanged(object sender, TimelineEventArgs e)
		{
            //regenerate background image with new settings
		}

		private void timeline_RulerSelectionChanged(object sender, TimelineEventArgs e)
		{
            //regenerate background image for selected frame
            Renderer r = new Renderer();
            Bitmap skinBmp = r.RenderBackground(fileMan.Movie, 
                new Size((int)(canvas1.Document.Width * canvas1.Zoom), (int)(canvas1.Document.Height * canvas1.Zoom)),
                e.OnionSkinFrame, e.LayerIndex, e.SelectedFrame);
            if (skinBmp != null) { canvas1.OnionSkin = skinBmp; }
		}

		#endregion

		#region CanvasEvents

		private void Canvas_SelectionChanged(object sender, EventArgs e)
		{
			if (colorDialog != null && !colorDialog.IsDisposed && canvas1.Mode == DrawingMode.Drawing)
			{
                if(canvas1.SelectedObjects.Count > 0 && canvas1.SelectedObjects[0].Type == XObjectType.Shape)
                {
                    Shape s = (Shape)canvas1.SelectedObjects[0];
                    colorDialog.LineColor = s.LineColor.GetColors();
                    colorDialog.FillColor = s.FillColor.GetColors();
                    colorDialog.LineSettings.Width = s.LineSettings.Width;
                }
			}

            if (motionDialog != null && !motionDialog.IsDisposed && canvas1.Mode == DrawingMode.Animation)
            {
                //load the animations for the selected object
				if (canvas1.SelectedObjects.Count > 0 && canvas1.SelectedObjects[0].Motions != null)
				{
					motionDialog.Motions = canvas1.SelectedObjects[0].Motions;
				}
            }
		}

		private void Canvas_ToolReset(object sender, EventArgs e)
		{
			UnCheckTools(btnPointer);
			btnPointer.Checked = true;
		}

		#endregion

		#region CanvasContextMenu

		private void menuCanvas_Opening(object sender, CancelEventArgs e)
        {
            if (canvas1.Mode == DrawingMode.Animation) { mnuAnimation.Visible = mnuAnimSeparator.Visible = true; }
            else { mnuAnimation.Visible = mnuAnimSeparator.Visible = false; }
		}

        private void mnuAnimSettings_Click(object sender, EventArgs e)
        {
            if (motionDialog == null || motionDialog.IsDisposed)
            {
                CreateMotionsDialog();
            }

			if(canvas1.SelectedObjects != null && canvas1.SelectedObjects.Count > 0)
			{
				motionDialog.Motions = canvas1.SelectedObjects[0].Motions;
			}
			
            motionDialog.Show(this);
        }

        private void CreateMotionsDialog()
        {
            motionDialog = new FormMotions();
            motionDialog.Owner = this;
            motionDialog.StartPosition = FormStartPosition.CenterScreen;
            //Point startPoint = new Point(this.Right - motionDialog.Width, this.Top);
            //startPoint.Offset(-15, 15);
           // motionDialog.Location = startPoint;
           // motionDialog.MotionChanged += new EventHandler(ColorDialog_ColorChanged);
        }

		private void mnuAnimLineFill_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimSize_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimOrder_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimSwap_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimMorphPath_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimMorph_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimFX_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimRotation_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimObjMotionPath_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimCamMotionPath_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimPtMotionPath_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimGrpMotionPath_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimSquash_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimStretch_Click(object sender, EventArgs e)
		{

		}

		private void mnuAnimClear_Click(object sender, EventArgs e)
		{

		}

		#endregion

		#region MainMenu
		
		private void mnuRender_Click(object sender, EventArgs e)
		{
			FormRender fr = new FormRender();
			fr.StartFrame = 1;
			fr.EndFrame = fileMan.Movie.GetTotalFrames();
			fr.Resolution = fileMan.Movie.Resolution;

			if (!String.IsNullOrEmpty(fileMan.FileName) && File.Exists(fileMan.FileName))
			{
				fr.OutputDirectory = Path.Combine(Path.GetDirectoryName(fileMan.FileName), "Output");
				if (!Directory.Exists(fr.OutputDirectory))
				{
					Directory.CreateDirectory(fr.OutputDirectory);
				}
			}
			DialogResult res = fr.ShowDialog(this);

			if (res == DialogResult.OK)
			{
				Renderer r = new Renderer();
				r.RenderPNG(fileMan.Movie, fr.Resolution, fr.OutputDirectory, fr.KeepLayers);
			}
		}

		private void mnuMovieProperties_Click(object sender, EventArgs e)
		{

		}		

		#endregion
	}
}
